$(function (){
//获取数据
    $.ajax({
        url: '/followed/list',
        type: 'GET',
        dataType: "json",
        // contentType: 'application/json;charset=UTF-8',
        // data: JSON.stringify(data),
        // async: true,
        processData: false,   // 不处理发送的数据
        contentType: false,   // 不设置Content-Type请求头
        success:
            function (res) {
                console.log(res);
                console.log(res.data.length);
                //加载
                init(res.data.followedVOList.length,res.data.followedVOList)
            }
    })

    //移除粉丝

    $(document).on('click','.btn',function (e) {

        var offOn = true;
        var $div = $('.btn');
        var $a=$('.btn-label');
        var index=$(this).closest('div').find('.id').text();
        var id=$(this).children('.id').text();
        console.log(id);
        $(".fchange").click(function () {
            console.log("删除")
            if (offOn) {

                $div.css('border-color', '#007bff');
                $a.css('color', '#007bff');
                $a.html('已移除');
                offOn = false;

                $.ajax({
                    url: '/followed/delete?id=' + id,
                    type: 'GET',
                    // data:index,
                    dataType: "json",
                    processData: false,   // 不处理发送的数据
                    contentType: false,   // 不设置Content-Type请求头
                    success:
                        function (res) {
                            console.log(res);
                            if (res.code == 200) {
                                $.toast({

                                    title: '通知',
                                    subtitle: '2秒前',
                                    content: "移除粉丝成功",
                                    type: 'success',
                                    delay: 3000,
                                    pause_on_hover: false
                                });

                                window.location.href = '/followed/index'
                            } else {
                                $.toast({
                                    title: '通知',
                                    subtitle: '2秒前',
                                    content: res.msg,
                                    type: 'error',
                                    delay: 1000,
                                    pause_on_hover: false
                                });
                            }
                        }
                })
            } else {

                $div.css('border-color', '#6c757d');
                $a.css('color', '#333');
                $a.html('移除粉丝');
                offOn = true;
            }
        })

    })
})

//跳转博主页
$(document).on('click','.skip',function (e){
    var index=$(this).closest('div').find('.id').text();
    console.log(index);
    window.location.href = '/unAuthorize/blogger?id='+index;

})

function init(num,data)
{
    for(let i=0;i<num;i++){
        var str = $('<tr>' +
            '<td style="width: 33.333333%" class="skip">'+data[i].username+'</td>'+
            '<td style="width: 33.333333%" class="skip">'+
            '<img alt="Avatar" class="table-avatar" src='+data[i].img+'></td>'+
            '<td style="width: 33.333333% ">' +
            '<a class="btn" style="border: 1px solid #6c757d;width: 100px" data-toggle="modal" data-target="#myModal">'+
            '<div style="display:none" class="id">'+data[i].followed_id +'</div>'+
            '<font class="btn-label" style="color: #6c757d;font-size: .875rem" >移除粉丝</font>'+
            '</a></td>'+
            '</tr>')
        $(".listbody").append(str);
    }
    $(".btn").each(function(){
        $(this).attr('id',"add"+$(this).index());
    })
}